<!--
*@tj-uploader
*@author lijiaqi
*@date 2023/12/21 15:17
-->
<template>
  <div class="upload_box">
    <p>{{ fileTitle }}</p>
    <van-uploader class="upload" v-model="fileList" :after-read="afterRead" :before-read="beforeRead">
      <!--      <template #preview-cover="{ file }">-->
      <!--        <div class="preview-cover van-ellipsis">{{ file.name }}</div>-->
      <!--      </template>-->
    </van-uploader>
  </div>
</template>

<script>
export default {
  name: "tj-uploader",
  props: {
    fileTitle: {
      type: String,
      default: '附件上传'
    }
  },
  components: {},
  data() {
    return {
      fileList: [],
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {
  },
  methods: {
    /**
     * 上传
     */
    afterRead(file) {
      console.log("上传上传上传上传", file)

    },
    changeFile(file) {
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = () => {
        // console.log('文件转换为Base64结果：' + reader.result)
        // this.iconBase64 = reader.result
        let a = {
          url: reader.result,
          name: file.name
        }
        this.fileList.push(a)
      }
      reader.onerror = function (error) {
        console.log('错误：', error)
      }
    },
    /**
     * 上传前处理
     */
    beforeRead(file) {
      console.log("前置-----", file)
      this.changeFile(file)
    },
  }
}
</script>

<style lang="less" scoped>
.upload_box {
  margin: 1.6rem;

  > p {
    color: rgb(29, 33, 41);
    font-family: HarmonyOS Sans SC;
    font-size: 16px;
    font-weight: 400;
    line-height: 19px;
    //letter-spacing: 0px;
    //text-align: center;
  }

  .upload {
    margin-top: 1.2rem;
  }
}

.preview-cover {
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
  width: 100%;
  padding: 4px;
  color: #fff;
  font-size: 12px;
  text-align: center;
  background: rgba(0, 0, 0, 0.3);
}
</style>
